package com.product.system.service.impl;

import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.lang.tree.TreeNodeConfig;
import cn.hutool.core.lang.tree.TreeUtil;
import com.product.system.domain.ArgpTradesmanCity;
import com.product.system.mapper.ArgpTradesmanCityMapper;
import com.product.system.service.ArgpTradesmanCityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ArgpTradesmanCityServicelmpl implements ArgpTradesmanCityService {//三级联动

    @Autowired
    ArgpTradesmanCityMapper argpTradesmanCityMapper;

    @Override
    public List<ArgpTradesmanCity> cityList() {
        return argpTradesmanCityMapper.cityList();
    }

    @Override
    public List<Tree<Object>> selectAllCity() {
        //创建TreeNodeConfig配置
        TreeNodeConfig treeNodeConfig = new TreeNodeConfig();
        //设置配置的属性
        treeNodeConfig.setIdKey("id");
        treeNodeConfig.setNameKey("cityName");
        treeNodeConfig.setDeep(4);
        treeNodeConfig.setParentIdKey("parentId");
        treeNodeConfig.setChildrenKey("children");
        //查询所有的部门
        List<ArgpTradesmanCity> city = cityList();
        //通过TreeUtil工具类进行转换树形结构
        List<Tree<Object>> build = TreeUtil.build(city, 1, treeNodeConfig, (treeNode, tree) -> {
            tree.setId(treeNode.getId());
            tree.setName(treeNode.getCityName());
            tree.setParentId(treeNode.getPid());//根据这个值进行树形结构处理
            //额外属性配置
            tree.putExtra("type",treeNode.getType());

        });

        return build;
    }
}
